home *** CD-ROM | disk | FTP | other *** search
- #include <Script.h>
- #include "kant load-save.h"
- #include "kant.h"
- #include "kant text twiddling.h"
- #include "file interface.h"
- #include "program globals.h"
- #include "error.h"
- #include "graphics.h"
- #include "dialogs.h"
- #include "environment.h"
- #include "util.h"
-
- FSSpec saveFile;
- Boolean deleteTheThing;
-
- void InitLoadSave(void)
- {
- saveFile.name[0]=0x00;
- deleteTheThing=FALSE;
- }
-
- void LoadSaveDispatch(Boolean isLoad, Boolean useOldFile)
- {
- if (isLoad)
- {
- if (GetSourceFile(&saveFile))
- HandleError(GetTheFile(&saveFile), FALSE);
- }
- else
- {
- useOldFile=useOldFile&&(saveFile.name[0]!=0x00);
- if (useOldFile ? TRUE : GetDestFile(&saveFile, &deleteTheThing, "\pSave text as..."))
- HandleError(SaveTheFile(saveFile), FALSE);
- }
- }
-
- enum ErrorTypes SaveTheFile(FSSpec saveFile)
- {
- OSErr theError;
- short thisFile;
- long count;
- TEHandle hTE;
-
- if (deleteTheThing)
- {
- FSpDelete(&saveFile);
- }
- FlushVol(0L, saveFile.vRefNum);
-
- theError=FSpCreate(&saveFile, CREATOR, SAVE_TYPE, smSystemScript);
- FlushVol(0L, saveFile.vRefNum);
-
- if (theError!=noErr)
- return kCantCreateFile;
-
- theError=FSpOpenDF(&saveFile, fsRdWrPerm, &thisFile);
-
- if (theError!=noErr)
- return kCantOpenFileToSave;
-
- hTE=GetTheTextHandle();
- count=(**hTE).teLength;
- theError=SetEOF(thisFile, count);
- if (theError!=noErr)
- {
- FSClose(thisFile);
- return kDiskFull;
- }
-
- SetFPos(thisFile, 1, 0L);
- theError=FSWrite(thisFile, &count, *((**hTE).hText));
-
- FSClose(thisFile);
- FlushVol(0L, saveFile.vRefNum);
-
- if (theError!=noErr)
- {
- FSpDelete(&saveFile);
-
- saveFile.name[0]=0x00;
- return kCantWriteFile;
- }
- else deleteTheThing=TRUE;
-
- SetWTitle(GetIndWindowPtr(kMainWindow), saveFile.name);
-
- return allsWell;
- }
-
- enum ErrorTypes GetTheFile(FSSpec *saveFile)
- {
- short thisFile;
- long count;
- OSErr theError;
- Ptr data;
-
- theError=FSpOpenDF(saveFile, fsRdPerm, &thisFile);
- if (theError!=noErr)
- return kCantOpenFileToLoad;
-
- GetEOF(thisFile, &count);
- if (count>32767)
- return kFileTooLarge;
-
- data=NewPtr(count);
- SetFPos(thisFile, 1, 0L);
- theError=FSRead(thisFile, &count, data);
- FSClose(thisFile);
-
- if (theError!=noErr)
- return kCantLoadFile;
-
- deleteTheThing=TRUE;
-
- OpenTheIndWindow(kMainWindow);
- SetWTitle(GetIndWindowPtr(kMainWindow), saveFile->name);
- SetTheText(data, count);
- DisposePtr(data);
-
- return allsWell;
- }
-